package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.GoodsService;
import service.TgoodsService;

public class MenuManageServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MenuManageServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		String type = request.getParameter("type");
		String result = "failed";
		// 添加商品
		if ("add".equals(action)) {
			String sellerId = request.getParameter("sellerId");
			String goodsName = request.getParameter("goodsName");
			String goodsClass = request.getParameter("goodsClass");
			String goodsPrice = request.getParameter("goodsPrice");
			String goodsDiscount = request.getParameter("goodsDiscount");
			String goodsKeywords = request.getParameter("goodsKeywords");
			String goodsDesc = request.getParameter("goodsDesc");
			String goodsUrl = request.getParameter("goodsUrl");
			if ("goods".equals(type)) {
				try {
					result = GoodsService.addGoods(sellerId, goodsName,
							goodsClass, goodsPrice, goodsDiscount, goodsUrl,
							goodsKeywords, goodsDesc);
				} catch (SQLException e) {
					e.printStackTrace();
					out.print(result);
					return;
				}
			} else if ("tgoods".equals(type)) {
				try {
					result = TgoodsService.addGoods(sellerId, goodsName,
							goodsClass, goodsPrice, goodsDiscount, goodsUrl,
							goodsKeywords, goodsDesc);
				} catch (SQLException e) {
					e.printStackTrace();
					out.print(result);
					return;
				}
			}
			// 修改商品信息
		} else if ("change".equals(action)) {
			if ("goods".equals(type)) {
				String sellerId = request.getParameter("sellerId");
				String goodsId = request.getParameter("goodsId");
				String goodsName = request.getParameter("goodsName");
				String goodsClass = request.getParameter("goodsClass");
				String goodsPrice = request.getParameter("goodsPrice");
				String goodsDiscount = request.getParameter("goodsDiscount");
				String goodsKeywords = request.getParameter("goodsKeywords");
				String goodsDesc = request.getParameter("goodsDesc");
				try {
					result = GoodsService.changeGoodsInfo(sellerId, goodsId,
							goodsName, goodsClass, goodsPrice, goodsDiscount,
							goodsKeywords, goodsDesc);
				} catch (SQLException e) {
					e.printStackTrace();
					out.print(result);
					return;
				}
			} else if ("tgoods".equals(type)) {

			}
			// 商品下架
		} else if ("offline".equals(action)) {
			String sellerId = request.getParameter("sellerId");
			String goodsId = request.getParameter("goodsId");
			if ("goods".equals(type)) {
				try {
					result = GoodsService.offlineGoods(sellerId, goodsId);
				} catch (SQLException e) {
					e.printStackTrace();
					out.print(result);
					return;
				}
			} else if ("tgoods".equals(type)) {
				try {
					result = TgoodsService.offlineGoods(sellerId, goodsId);
				} catch (SQLException e) {
					e.printStackTrace();
					out.print(result);
					return;
				}
			}
			// 已下架的商品重新上架
		} else if ("online".equals(action)) {
			if ("goods".equals(type)) {
				String sellerId = request.getParameter("sellerId");
				String goodsId = request.getParameter("goodsId");
				try {
					result = GoodsService.onlineGoods(sellerId, goodsId);
				} catch (SQLException e) {
					e.printStackTrace();
					out.print(result);
					return;
				}
			} else if ("tgoods".equals(type)) {

			}
		}
		out.print(result);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
